package com.zking.frame;

import com.zking.dao.FoodDao;
import com.zking.dao.UserDao;
import com.zking.entity.Food;
import com.zking.entity.User;
import com.zking.utils.Tool工具;

import javax.swing.*;
import javax.swing.border.MatteBorder;
import javax.swing.event.AncestorEvent;
import javax.swing.event.AncestorListener;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.util.List;

public class Administrators extends JFrame {
    // 书写标题，书写店名提示，验证码提示,用户名，用户密码，用户手机号
    private JLabel textJLabel,shopJLabel, codeJLabel,userJLabel,passwordJLabel,phoneJLabel;
    private JTextField shopfField,codeField,userField, passwordField,phoneField;// 文本框
    private JButton landButton, registerButton;// 登陆注册按钮
    private JPanel jPanel;
    private FoodDao foodDao = new FoodDao();
    private UserDao userDao = new UserDao();
    private  boolean aa = true , bb =false,cc=false,dd=false,ee=false,ff=false;
    public Administrators(JFrame a){
        a.dispose();
        JFrame jFrame = Tool工具.getJFrame();
        jFrame.getContentPane().setBackground(Color.white);

        jPanel = new JPanel();
        jPanel.setBounds(20, 50, 600, 900);
        jPanel.setLayout(null);
        jPanel.setBorder(new MatteBorder(2, 2, 2, 2, new Color(63, 247, 18)));
        jFrame.add(jPanel, 0);

        // 加入背景
        ImageIcon icon = new ImageIcon(Administrators.class.getResource("注册背景.jpg"));
        JLabel jLabel = new JLabel(icon);
        jLabel.setBounds(0, 0, 600, 900);
        jPanel.add(jLabel, -1);

        //设置提示
        textJLabel = new JLabel("注册管理员",0);
        textJLabel.setBounds(150,100,300,50);
        textJLabel.setFont(new Font("黑体",Font.BOLD,40));
        jPanel.add(textJLabel,0);

        shopJLabel = new JLabel("商铺名称：",4);
        shopJLabel.setBounds(10,260,230,50);
        shopJLabel.setFont(new Font("黑体",Font.BOLD,30));
        jPanel.add(shopJLabel,0);

        codeJLabel = new JLabel("管理员验证码：",4);
        codeJLabel.setBounds(10,340,230,50);
        codeJLabel.setFont(new Font("黑体",Font.BOLD,30));
        jPanel.add(codeJLabel,0);

        userJLabel = new JLabel("用户名：",4);
        userJLabel.setBounds(10,420,230,50);
        userJLabel.setFont(new Font("黑体",Font.BOLD,30));
        jPanel.add(userJLabel,0);

        passwordJLabel = new JLabel("密码：",4);
        passwordJLabel.setBounds(10,500,230,50);
        passwordJLabel.setFont(new Font("黑体",Font.BOLD,30));
        jPanel.add(passwordJLabel,0);

        phoneJLabel = new JLabel("电话号码：",4);
        phoneJLabel.setBounds(10,580,230,50);
        phoneJLabel.setFont(new Font("黑体",Font.BOLD,30));
        jPanel.add(phoneJLabel,0);


        shopfField = new JTextField();
        shopfField.setBounds(250,260,300,50);
        shopfField.setFont(new Font("黑体",Font.BOLD,20));
        jPanel.add(shopfField,0);

        codeField = new JTextField();
        codeField.setBounds(250,340,300,50);
        codeField.setFont(new Font("黑体",Font.BOLD,20));
        jPanel.add(codeField,0);

        userField = new JTextField();
        userField.setBounds(250,420,300,50);
        userField.setFont(new Font("黑体",Font.BOLD,20));
        jPanel.add(userField,0);

        passwordField = new JTextField();
        passwordField.setBounds(250,500,300,50);
        passwordField.setFont(new Font("黑体",Font.BOLD,20));
        jPanel.add(passwordField,0);

        phoneField = new JTextField();
        phoneField.setBounds(250,580,300,50);
        phoneField.setFont(new Font("黑体",Font.BOLD,20));
        jPanel.add(phoneField,0);

        landButton = new JButton("返回登陆");
        landButton.setBounds(140,660,150,50);
        landButton.setFont(new Font("黑体",Font.BOLD,20));
        jPanel.add(landButton,0);
        landButton.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                new Land(jFrame);
            }
        });

        registerButton  = new JButton("注册");
        registerButton.setBounds(310,660,150,50);
        registerButton.setFont(new Font("黑体",Font.BOLD,20));
        jPanel.add(registerButton,0);

        //设置聚焦失焦事件
        //商铺分类

        //设置聚焦失焦事件
        //商铺名称
        shopfField.addFocusListener(new FocusListener() {
            @Override
            public void focusGained(FocusEvent focusEvent) {
                // TODO Auto-generated method stub
                if (shopfField.getText().equals("请输入商铺名称") || shopfField.getText().equals("商铺名不存在")||
                        shopfField.getText().equals("商铺名已注册")) {
                    shopfField.setText("");
                    shopfField.setForeground(Color.gray);
                }

            }
            @Override
            public void focusLost(FocusEvent focusEvent) {
                if (shopfField.getText().equals("")) {
                    shopfField.setText("请输入商铺名称");
                    shopfField.setForeground(Color.gray);
                    bb = false;
                } else {
                    List<Food> foods = foodDao.queryShop(shopfField.getText());
                    List<User> users = userDao.queryStudents();
                    int a = 0;
                    for (int b=0;b<users.size();b++){
                        if (users.get(b).getSort().equals(shopfField.getText())){
                            a++;
                            break;
                        }
                    }
                    if (foods.size()==0) {
                        shopfField.setText("商铺名不存");
                    } else {
                       if (a!=0){
                           shopfField.setText("商铺名已注册");
                       }else{
                           bb=true;
                       }
                    }
                }
            }
        });
        //设置聚焦失焦事件
        //管理员验证码
        codeField.addFocusListener(new FocusListener() {
            @Override
            public void focusGained(FocusEvent focusEvent) {
                // TODO Auto-generated method stub
                if (codeField.getText().equals("请输入验证码") || codeField.getText().equals("请输入正确的验证码")) {
                    codeField.setText("");
                    codeField.setForeground(Color.gray);
                }

            }
            @Override
            public void focusLost(FocusEvent focusEvent) {
                if (codeField.getText().equals("")) {
                    codeField.setText("请输入验证码");
                    codeField.setForeground(Color.gray);
                    cc = false;
                } else {
                    if (!codeField.getText().matches("0000")) {
                        codeField.setText("请输入正确的验证码");
                        return;
                    } else {
                        cc = true;
                    }
                }
            }
        });
        //设置聚焦失焦事件
        //用户名
        userField.addFocusListener(new FocusListener() {
            @Override
            public void focusGained(FocusEvent focusEvent) {
                // TODO Auto-generated method stub
                if (userField.getText().equals("请输入用户名") || userField.getText().equals("只能为数字、字母,6至12位")||
                        userField.getText().equals("用户名已存在")) {
                    userField.setText("");
                    userField.setForeground(Color.gray);
                }

            }
            @Override
            public void focusLost(FocusEvent focusEvent) {
                List<User> users = userDao.queryStudents();
                int a = 0;
                for (int b=0;b<users.size();b++){
                    if (users.get(b).getUserName().equals(userField.getText())){
                        a++;
                        System.out.println(users.get(b)+""+a);
                        break;
                    }
                }
                if (userField.getText().equals("")) {
                    userField.setText("请输入用户名");
                    userField.setForeground(Color.gray);
                    dd = false;
                } else {
                    if (!userField.getText().matches("^[0-9a-zA-Z]{6,12}$")) {
                        userField.setText("只能为数字、字母,6至12位");


                        return;
                    }else{
                        if (a!=0){
                            System.out.println(a);
                            userField.setText("用户名已存在");
                        }else{
                            dd = true;
                        }
                    }
                }
            }
        });
        //设置聚焦失焦事件
        //密码
        passwordField.addFocusListener(new FocusListener() {
            @Override
            public void focusGained(FocusEvent focusEvent) {
                // TODO Auto-generated method stub
                if ( passwordField.getText().equals("请输入密码") ||  passwordField.getText().equals("只能为数字、字母,6至12位")) {
                    passwordField.setText("");
                    passwordField.setForeground(Color.gray);
                }

            }
            @Override
            public void focusLost(FocusEvent focusEvent) {
                if ( passwordField.getText().equals("")) {
                    passwordField.setText("请输入密码");
                    passwordField.setForeground(Color.gray);
                    ee= false;
                } else {
                    if (! passwordField.getText().matches("^[0-9a-zA-Z]{6,12}$")) {
                        passwordField.setText("只能为数字、字母,6至12位");
                        return;
                    } else {
                        ee = true;
                    }
                }
            }
        });
        //设置聚焦失焦事件
        //电话号码
        phoneField.addFocusListener(new FocusListener() {
            @Override
            public void focusGained(FocusEvent focusEvent) {
                // TODO Auto-generated method stub
                if ( phoneField.getText().equals("请输入电话号码") || phoneField.getText().equals("请输入正确的电话号码")) {
                    phoneField.setText("");
                    phoneField.setForeground(Color.gray);
                }

            }
            @Override
            public void focusLost(FocusEvent focusEvent) {
                if (phoneField.getText().equals("")) {
                    phoneField.setText("请输入电话号码");
                    phoneField.setForeground(Color.gray);
                    ff= false;
                } else {
                    if (! phoneField.getText().matches("^1[3|4|5|7|8][0-9]\\d{4,8}$")) {
                        phoneField.setText("请输入正确的电话号码");
                        return;
                    } else {
                        ff = true;
                    }
                }
            }
        });
        registerButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                if (registerButton.getText().equals("注册")){
                    if( bb&&cc&&dd&&ee&&ff) {
                        User user = new User();
                        user.setSort(shopfField.getText());
                        user.setUserName(userField.getText());
                        user.setPassWord(passwordField.getText());
                        user.setPhoneNumber(phoneField.getText());
                        User user2 = new User();
                        user.setShopname(foodDao.querySort(shopfField.getText()));
                        System.out.println(userDao.insertStudent(user));
                        registerButton.setText("注册成功");
                        dispose();
                    }
                }
            }
        });
        // 加载并刷新面板
        jFrame.repaint();
        jFrame.revalidate();
    }

}
